package com.fyi.controller;

import com.fyi.entity.Admin;
import com.fyi.entity.Article;
import com.fyi.entity.User;
import com.fyi.service.AdminService;
import com.fyi.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController{

    private static final Log logger = LogFactory.getLog(UserController.class);

    @Autowired
    private UserService userService;


    /**
     * 登录
     */
    @RequestMapping("loginView")
    public String login(){
        return "login";
    }
    @RequestMapping("/login")
    public String login(Integer loginId, String password , HttpSession session, Model model){
        User login = userService.login(loginId, password);
        if (login!=null) {
            logger.info("登录成功");
            //把用户数据保存在session域对象中
            session.setAttribute("User", login);

            return "articleIndex";
        }else {
            logger.info("登录失败！");
            model.addAttribute("messageError","账号ID或密码错误");
            return "login";
        }
    }
    /**
     * 安全退出
    /* *//*
    @RequestMapping("/exit")
    public String exit(HttpSession session){
        session.removeAttribute("User");
        return "redirect:/";
    }*/

    /**
     * 注册
     */
    @RequestMapping("registerView")
    public String register(Model model){
        model.addAttribute("user",new User());
        return "register";
    }
    @RequestMapping("/register")
    public String register(@Valid @ModelAttribute User user, BindingResult result){
        if (result.hasErrors()){
            return "register";
        }
        Date d=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        String time=sdf.format(d);
        System.out.println("--"+time);
        user.setCreateUserTime(time);

        Integer addUser = userService.addUser(user);
        logger.info("注册成功");
        return "registerSucceed";
    }
    /**
     * 页面跳转
     */



    /**
     * 查看所有用户
    /* */
    @RequestMapping("showUserView")
    public String superAdminView(){
        return "backView/showUser";
    }
    @RequestMapping("/selectAllUser")
    @ResponseBody
    public Map selectAllUser(){
        List<User> selectAllUser = userService.selectAllUser();
        System.out.println(selectAllUser);
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "查询成功");
        result.put("data", selectAllUser);
        return result;
    }
    /**
     * 根据Id查询
     */
    @RequestMapping("/selectUserById/{userId}")
    @ResponseBody
    public Map selectUserById(@PathVariable Integer userId){
        User selectById = userService.selectById(userId);
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "查询成功");
        result.put("data", selectById);
        return result;
    }
    /**
     * 删除
     */
    @RequestMapping("/deleteUser/{userId}")
    @ResponseBody
    public Map deleteAdmin(@PathVariable Integer userId){
        Integer deleteUser = userService.deleteUser(userId);
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "删除成功");
        result.put("data", deleteUser);
        return result;
    }
    /**
     * 修改
     */
    /*@RequestMapping("updateUser")
    @ResponseBody
    public Map updateArticle(@ModelAttribute User user){

        Integer updateUser = userService.updateUser(user);
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "修改成功");
        result.put("data", updateUser);
        return result;
    }*/
}
